<?php
require_once('BaseStatsReportForm.class.php');

class EmployeeLeavesComparisons extends BaseStatsReportForm
{

	public function getOrderByFields(){
		return array (
		EmployeePeer::CODE  => 'employee code'
		, EmployeeI18nPeer::FIRST_NAME  => 'name'
		, EmployeePeer::CARD_ID  => 'card id'
		, EmployeePeer::HIRING_DATE  => 'hiring_date'
		);
	}


	public $display_fields = array(
	'serial'=>'Serial'
	,'employee_code'=>'Employee Code'
	,'employee_name'=>'Employee name'
	,'month_year'=>'Month/Year'
	,'total_period'=>'Total period'
	);

	public  function getDisplayFields()
	{
		$fields = array();
		foreach (CodeLeaveTypePeer::getAll(array(),$without_non_calclated = true) as $leave_type){
			$fields['leave_type_'.$leave_type->getId()] = $leave_type->__toString();
		}
		return array_merge( $this->display_fields , $fields );
	}



	public function configure(){
		parent::configure();




		$this->widgetSchema['compare_month_struct'] = new sfWidgetFormMonthCompare();
		$this->validatorSchema['compare_month_struct'] = new sfvalidatorFormMonthCompare();
		$this->getWidgetSchema()->setLabel('compare_month_struct' , 'Compare Month');


		/*        $this->widgetSchema['compare_month_struct'] = new sfWidgetFormInput();
		$this->validatorSchema['compare_month_struct'] = new sfValidatorString();
		$this->getWidgetSchema()->setLabel('compare_month_struct' , 'Compare Month');
		*/


		$this->getWidgetSchema()->moveField('section_id','before','employee_id');
		$this->getWidgetSchema()->moveField('department_id','before','section_id');
		$this->getWidgetSchema()->setDefault('group_by','all');


		$this->getWidgetSchema()->moveField('order_by' ,sfWidgetFormSchema::LAST);

		unset($this['leave_type_id'],$this['leave_start'],$this['leave_end']);

	}

	protected function addCompareMonthStructColumnCriteria(Criteria $criteria, $field, $value=null){

		if(is_array($value) && is_array($value['compare_with_month']) && count($value['compare_with_month']) > 0 ){
		    //die($value['compare_with_month']);
		    $ind = array_search($value['compare_month'] , $value['compare_with_month']);
		    unset($value['compare_with_month'][$ind]);
		    
			//$criteria->addJoin(LinkEmployeeLeavePeer::LEAVE_TYPE_ID  ,TransactionPeer::LEAVE_TYPE_ID );
			$criteria->add(LinkEmployeeLeavePeer::MONTH ,$value['compare_month']);
			$criteria->add(LinkEmployeeLeavePeer::YEAR  ,date('Y'));
			sfConfig::set('compared_months',$value['compare_with_month']);
			sfConfig::set('compared_year',$value['compare_with_year']);
		}
	}
	protected function addOrderByColumnCriteria(Criteria $criteria, $field, $value){
		//die('sasd');
		foreach ($value as $v){
			if($v == EmployeeI18nPeer::FIRST_NAME){
				$criteria->addJoin(LinkEmployeeLeavePeer::EMPLOYEE_ID , EmployeePeer::ID );
				$criteria->addJoin(EmployeePeer::ID , EmployeeI18nPeer::ID, Criteria::RIGHT_JOIN  );
				//$criteria->add(EmployeeI18nPeer::CULTURE ,sfContext::getInstance()->getUser()->getCulture() );
			}
			if($v == EmployeePeer::HIRING_DATE ){
				$criteria->addJoin(LinkEmployeeLeavePeer::EMPLOYEE_ID , EmployeePeer::ID );
				//$criteria->addJoin(EmployeePeer::ID , EmployeeI18nPeer::ID, Criteria::RIGHT_JOIN  );
				//$criteria->add(EmployeeI18nPeer::CULTURE ,sfContext::getInstance()->getUser()->getCulture() );
			}
		}

		parent::addOrderByColumnCriteria( $criteria, $field, $value);
	}
	
	
    protected function addGroupByColumnCriteria ( Criteria $criteria , $field , $value = null ) {

        //die('fg');
        parent::addGroupByColumnCriteria( $criteria , $field , $value );
        
/*        switch ($value) {
            case EmployeePeer::SECTION_ID:

                if($this->getModelName()!='Employee'){
                    $criteria->addJoin(constant($this->getModelName().'Peer::EMPLOYEE_ID'),EmployeePeer::ID);
                }
                $criteria->addJoin($value,CodeSectionPeer::ID);
                $criteria->addAscendingOrderByColumn(CodeSectionPeer::ID);
                sfConfig::set('group_by' , $this->getGroupByFields($value) );

                break;
            case EmployeePeer::DEPARTMENT_ID :
                if($this->getModelName()!='Employee'){
                    $criteria->addJoin(constant($this->getModelName().'Peer::EMPLOYEE_ID'),EmployeePeer::ID);
                }
                $criteria->addJoin( $value , CodeDepartmentPeer::ID );
                $criteria->addAscendingOrderByColumn(CodeDepartmentPeer::ID);
                sfConfig::set('group_by' , $this->getGroupByFields($value) );
                break;
            default:
                break;
        }*/


    }
	

}
